package com.airbnb.epoxy.models;

import butterknife.BindComponent;
import com.airbnb.epoxy.EpoxyAttribute;
import com.airbnb.epoxy.EpoxyModelClass;
import com.airbnb.epoxy.EpoxyModelWithHolder;
import com.airbnb.epoxy.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

import static com.airbnb.epoxy.EpoxyAttribute.Option.DoNotHash;

@EpoxyModelClass(layout = ResourceTable.Layout_model_image_button)
public abstract class ImageButtonModel extends EpoxyModelWithHolder<ImageButtonModel.ImageButtonHolder> {
  @EpoxyAttribute /*@ElementRes */int imageRes;
  @EpoxyAttribute(DoNotHash)
  Component.ClickedListener clickListener;

  @Override
  public void bind(ImageButtonHolder holder) {
    holder.button.setImageAndDecodeBounds(imageRes);
    holder.button.setClickedListener(clickListener);
  }

  @Override
  public void unbind(ImageButtonHolder holder) {
    // Release resources and don't leak listeners as this view goes back to the view pool
    holder.button.setClickedListener(null);
    holder.button.setImageElement(null);
  }

  static class ImageButtonHolder extends BaseEpoxyHolder {
    @BindComponent(ResourceTable.Id_button)
    Image button;
  }
}
